package com.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bean.ProductionPlan;
import com.bean.ProductionPlanFood;
import com.bean.User;
import com.service.ProductionPlanFoodService;
import com.service.ProductionPlanService;
import com.util.TimeUtil;

/**
 * @author author : 张鹏
 * @date createTime：2018年1月19日 下午5:54:08
 * @Description: 该类的功能描述
 */
@Controller
@RequestMapping("productionPlan")
public class ProductionPlanController {
	@Autowired
	private ProductionPlanService planService;
	@Autowired
	private ProductionPlanFoodService planFoodService;

	private ProductionPlan plan;
	
	/**
	 * 根据状态码查询生产计划
	 * @param state  状态码
	 * @return
	 */
	@RequestMapping("getProductionPlanByStateAjax")
	@ResponseBody
	public List<ProductionPlan> getProductionPlanByStateAjax(int state){
		return planService.selectByState(state);
	}
	
	@RequestMapping("deleteProductionPlanByProductionPlanIdAjax")
	@ResponseBody
	public int deleteProductionPlanByProductionPlanIdAjax(int productionPlanId){
		return planService.deleteProductionPlan(productionPlanId);
	}
	
	
	
	/**
	 * 得到生产计划中的  各个菜品的对应的生产总和
	 * @param state
	 * @return
	 */
	@RequestMapping("getProductionPlanFoodAndSumQuantityAjax")
	@ResponseBody
	public List<ProductionPlanFood> getProductionPlanFoodAndSumQuantityAjax(){
		return planFoodService.getProductionPlanFoodAndSumQuantity();
	}
	
	/**
	 * 得到每天的生产计划的成本总额
	 * @return
	 */
	@RequestMapping("getAllProductionPlanGroupByDayProductTimeAjax")
	@ResponseBody
	public List<ProductionPlan> getAllProductionPlanGroupByDayProductTimeAjax(){
		return planService.selectAllGroupByDayProductTime();
	}
	/**
	 * 得到每月的生产计划的成本总额
	 * @return
	 */
	@RequestMapping("getAllProductionPlanGroupByMonthProductTimeAjax")
	@ResponseBody
	public List<ProductionPlan> getAllProductionPlanGroupByMonthProductTimeAjax(){
		return planService.selectAllGroupByMonthProductTime();
	}
	/**
	 * 得到每年的生产计划的成本总额
	 * @return
	 */
	@RequestMapping("getAllProductionPlanGroupByYearProductTimeAjax")
	@ResponseBody
	public List<ProductionPlan> getAllProductionPlanGroupByYearProductTimeAjax(){
		return planService.selectAllGroupByYearProductTime();
	}
	
	
	
	@RequestMapping("getAllProductionPlanAjax")
	@ResponseBody
	public List<ProductionPlan> getAllProductionPlanAjax(){
		return planService.selectAll();
	}
	
	/**
	 * 审核生产计划
	 * @param productionPlanId 生产计划id
	 * @param exame 审核情况  2为审核成功  3为审核失败
	 * @param exameSuggestion 审核意见
	 * @return
	 */
	@RequestMapping("exameProductionPlanAjax")
	@ResponseBody
	public String exameProductionPlanAjax(int productionPlanId,int exame,String exameSuggestion,HttpServletRequest request){
		//int userId=(int) request.getSession().getAttribute("userId");
		int userId=((User)request.getSession().getAttribute("userInfo")).getUserId();
		plan=new ProductionPlan();
		plan.setExameById(userId);
		plan.setProductionPlanId(productionPlanId);
		plan.setExameTime(TimeUtil.formatTime());
		plan.setExameSuggestion(exameSuggestion);
		plan.setState(exame+"");
		
		return planService.updatePlan(plan)+"";
	}
	
	
	/**
	 * 根据生产计划id 得到生产计划商品表的 数据
	 * @param productionPlanId
	 * @param returnTo 返回的网址
	 * @param remark  标记  exame表示显示在审核页面   show 表示显示在查看的页面
	 * @return
	 */
	@RequestMapping("getProductionPlanByPrimaryKeyReturnTo")
	public String getProductionPlanByPrimaryKeyReturnTo(int productionPlanId,String returnTo,String remark,HttpServletRequest request){
		//生产计划数据
		request.setAttribute("productionPlan", planService.selectByPrimaryKey(productionPlanId));
		//生产计划对应的商品
		request.setAttribute("productionPlanFoodList", planFoodService.getByProductionPlanId(productionPlanId));
		
		request.setAttribute("remark", remark);
		return returnTo;
	}
	

	/**
	 * 制定生产计划
	 * 
	 * @param productTime
	 *            生产计划执行时间
	 * @param createById
	 *            创建者id
	 * @param remark
	 *            备注
	 * @param productionPlanFoodList
	 *            生产计划对应的菜品表 json格式
	 * @param request
	 * @return
	 */
	@RequestMapping("createNewProductionPlanAjax")
	@ResponseBody
	public int createNewProductionPlanAjax(String productTime, 
			String remark, String breakfastList, String lunchList,
			String dinnerList,Double totalPrice, HttpServletRequest request) {
		int userId=((User)request.getSession().getAttribute("userInfo")).getUserId();
		plan = new ProductionPlan();
		plan.setProductTime(productTime);
		plan.setCreateById(userId);
		plan.setRemark(remark);
		plan.setCreateTime(TimeUtil.formatTime());
		plan.setState("1");//设置状态为已创建 待审核
		plan.setTotalPrice(totalPrice);
		int result=planService.createNewPlan(plan);
		
		int productionPlanId=plan.getProductionPlanId();
		//处理生产计划对应的菜品list
		//处理早餐
		if (!"".equals(breakfastList) && breakfastList != null
				&& !"[]".equals(breakfastList)) {
			ProductionPlanFood planFood;
			JSONArray breakfast = JSONArray.fromObject(breakfastList);
			JSONObject jobj;
			for (int i = 0; i < breakfast.size(); i++) {
				planFood=new ProductionPlanFood();
				jobj = (JSONObject) breakfast.get(i);
				
				planFood.setFoodId( jobj.getInt("foodId"));
				planFood.setMealTime("1");
				planFood.setProductionPlanId(productionPlanId);
				planFood.setProductById(jobj.getInt("productById") );
				planFood.setQuantity(jobj.getInt("foodQuantity"));
				planFoodService.createNewProductionPlanFood(planFood);
			}
		}
		
		//处理午餐
		if (!"".equals(lunchList) && lunchList != null
				&& !"[]".equals(lunchList)) {
			ProductionPlanFood planFood;
			JSONArray lunch = JSONArray.fromObject(lunchList);
			JSONObject jobj;
			for (int i = 0; i < lunch.size(); i++) {
				planFood=new ProductionPlanFood();
				jobj = (JSONObject) lunch.get(i);
				
				planFood.setFoodId(jobj.getInt("foodId"));
				planFood.setMealTime("2");
				planFood.setProductionPlanId(productionPlanId);
				planFood.setProductById( jobj.getInt("productById"));
				planFood.setQuantity( jobj.getInt("foodQuantity"));
				planFoodService.createNewProductionPlanFood(planFood);
			}
		}
		
		//处理晚餐
		if (!"".equals(dinnerList) && dinnerList != null
				&& !"[]".equals(dinnerList)) {
			ProductionPlanFood planFood;
			JSONArray dinner = JSONArray.fromObject(dinnerList);
			JSONObject jobj;
			for (int i = 0; i < dinner.size(); i++) {
				planFood=new ProductionPlanFood();
				jobj = (JSONObject) dinner.get(i);
				
				planFood.setFoodId(jobj.getInt("foodId"));
				planFood.setMealTime("3");
				planFood.setProductionPlanId(productionPlanId);
				planFood.setProductById(jobj.getInt("productById"));
				planFood.setQuantity(jobj.getInt("foodQuantity"));
				planFoodService.createNewProductionPlanFood(planFood);
			}
		}
		return result;
	}
 
}
